package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ParkingRateMapper;
import com.ruoyi.system.domain.ParkingRate;
import com.ruoyi.system.service.IParkingRateService;

/**
 * 停车费Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-28
 */
@Service
public class ParkingRateServiceImpl implements IParkingRateService {
    @Autowired
    private ParkingRateMapper parkingRateMapper;

    /**
     * 查询停车费
     *
     * @param prId 停车费ID
     * @return 停车费
     */
    @Override
    public ParkingRate selectParkingRateById(Long prId) {
        return parkingRateMapper.selectParkingRateById(prId);
    }

    /**
     * 查询停车费列表
     *
     * @param parkingRate 停车费
     * @return 停车费
     */
    @Override
    public List<ParkingRate> selectParkingRateList(ParkingRate parkingRate) {
        return parkingRateMapper.selectParkingRateList(parkingRate);
    }

    /**
     * 每天账单
     * @param parkingRate
     * @return
     */
    @Override
    public List ParkingRateList(ParkingRate parkingRate) {
        List list= new ArrayList();
        HashMap map= new HashMap();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-mm-dd");
            List<Map<String, String>> maps = parkingRateMapper.AllDay(parkingRate);
            for (int i = 0; i < maps.size(); i++) {
                Date time = simpleDateFormat.parse(maps.get(i).get("time"));
                parkingRate.setPrCreatetime(time);
                List<ParkingRate> parkingRates = parkingRateMapper.DayList(parkingRate);
                map.put("time",maps.get(i).get("time"));
                map.put("List",parkingRates);
                list.add(map);
            }
        } catch (ParseException parseException) {

        }
        return list;
    }

    /**
     * 新增停车费
     *
     * @param parkingRate 停车费
     * @return 结果
     */
    @Override
    public int insertParkingRate(ParkingRate parkingRate) {
        parkingRate.setCreateTime(DateUtils.getNowDate());
        return parkingRateMapper.insertParkingRate(parkingRate);
    }

    /**
     * 修改停车费
     *
     * @param parkingRate 停车费
     * @return 结果
     */
    @Override
    public int updateParkingRate(ParkingRate parkingRate) {
        parkingRate.setUpdateTime(DateUtils.getNowDate());
        if (null != parkingRate.getPrEndtime()) {
            int min = parkingRateMapper.queeryMinute(parkingRate);
            int a = min / 60;
            int b = min % 60;
            if (b == 0) {
                parkingRate.setPrTime(new BigDecimal(a));
            } else if (b % 30 == 0) {
                parkingRate.setPrTime(new BigDecimal(a + 0.5));
            } else if (b % 30 != 0) {
                parkingRate.setPrTime(new BigDecimal(a + 1));
            }
        }
        return parkingRateMapper.updateParkingRate(parkingRate);
    }

    /**
     * 批量删除停车费
     *
     * @param prIds 需要删除的停车费ID
     * @return 结果
     */
    @Override
    public int deleteParkingRateByIds(Long[] prIds) {
        return parkingRateMapper.deleteParkingRateByIds(prIds);
    }

    /**
     * 删除停车费信息
     *
     * @param prId 停车费ID
     * @return 结果
     */
    @Override
    public int deleteParkingRateById(Long prId) {
        return parkingRateMapper.deleteParkingRateById(prId);
    }
}
